package com.warehouse.controller;

import com.warehouse.common.Result;
import com.warehouse.entity.ProductCategory;
import com.warehouse.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类Controller
 */
@RestController
@RequestMapping("/api/categories")
@CrossOrigin(origins = "*")
public class ProductCategoryController {
    
    @Autowired
    private ProductCategoryService productCategoryService;
    
    /**
     * 获取所有启用的分类
     */
    @GetMapping("/enabled")
    public Result<List<ProductCategory>> getEnabledCategories() {
        try {
            List<ProductCategory> categories = productCategoryService.getAllEnabledCategories();
            return Result.success(categories);
        } catch (Exception e) {
            return Result.error("获取分类列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取所有分类
     */
    @GetMapping
    public Result<List<ProductCategory>> getAllCategories() {
        try {
            List<ProductCategory> categories = productCategoryService.getAllCategories();
            return Result.success(categories);
        } catch (Exception e) {
            return Result.error("获取分类列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据ID获取分类
     */
    @GetMapping("/{id}")
    public Result<ProductCategory> getCategoryById(@PathVariable Long id) {
        try {
            ProductCategory category = productCategoryService.getCategoryById(id);
            if (category != null) {
                return Result.success(category);
            } else {
                return Result.error("分类不存在");
            }
        } catch (Exception e) {
            return Result.error("获取分类失败: " + e.getMessage());
        }
    }
    
    /**
     * 添加分类
     */
    @PostMapping
    public Result<String> addCategory(@RequestBody ProductCategory category) {
        try {
            if (category.getCategoryName() == null || category.getCategoryName().trim().isEmpty()) {
                return Result.error("分类名称不能为空");
            }
            
            if (productCategoryService.addCategory(category)) {
                return Result.success("添加分类成功");
            } else {
                return Result.error("分类名称已存在");
            }
        } catch (Exception e) {
            return Result.error("添加分类失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新分类
     */
    @PutMapping("/{id}")
    public Result<String> updateCategory(@PathVariable Long id, @RequestBody ProductCategory category) {
        try {
            category.setId(id);
            
            if (category.getCategoryName() == null || category.getCategoryName().trim().isEmpty()) {
                return Result.error("分类名称不能为空");
            }
            
            if (productCategoryService.updateCategory(category)) {
                return Result.success("更新分类成功");
            } else {
                return Result.error("更新分类失败，分类名称可能已存在");
            }
        } catch (Exception e) {
            return Result.error("更新分类失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除分类
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteCategory(@PathVariable Long id) {
        try {
            if (productCategoryService.deleteCategory(id)) {
                return Result.success("删除分类成功");
            } else {
                return Result.error("删除分类失败");
            }
        } catch (Exception e) {
            return Result.error("删除分类失败: " + e.getMessage());
        }
    }
    
    /**
     * 检查分类名称是否存在
     */
    @GetMapping("/check-name")
    public Result<Boolean> checkCategoryName(@RequestParam String categoryName) {
        try {
            boolean exists = productCategoryService.isCategoryNameExists(categoryName);
            return Result.success(exists);
        } catch (Exception e) {
            return Result.error("检查分类名称失败: " + e.getMessage());
        }
    }
} 